//
//  drawerViewController.m
//  Delicious
//
//  Created by gy on 16/5/5.
//  Copyright © 2016年 gy. All rights reserved.
//

#define kOffSet 0.7 //mainvc的偏移量
#import "drawerViewController.h"

@interface drawerViewController ()

@property (nonatomic, strong) UIView *leftView;



@end

@implementation drawerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//初始化抽屉  leftVC(左边的抽屉)  MainVC(主界面)
- (instancetype)initWithLeftVC:(UIViewController *)leftVC mainVC:(UIViewController *)mainVC{
    self = [super init];
    if (self) {
        [self addChildViewController:leftVC];
        [self addChildViewController:mainVC];
        _leftView = leftVC.view;
        _mainView = mainVC.view;
        [self.view addSubview:leftVC.view];
        [self.view addSubview:mainVC.view];
        self.pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panHander:)];
        [self.mainView addGestureRecognizer:self.pan];


    }
    
    return self;
}

- (void)panHander:(UIPanGestureRecognizer *)pan{
    CGPoint panCenter = [pan translationInView:self.mainView];
    CGRect frame = self.mainView.frame;
    frame.origin.x += panCenter.x;
    if (frame.origin.x > 0) {
        self.mainView.frame = frame;
    }
    [pan setTranslation:CGPointZero inView:self.mainView];
    if (pan.state == UIGestureRecognizerStateEnded) {
        if (self.mainView.frame.origin.x > kScreenWidth * kOffSet / 2) {
            [self open];
        } else{
            [self close];
        }
    }
}

//打开抽屉
- (void)open{
    CGRect mainFrame = _mainView.frame;
    mainFrame.origin.x = kScreenWidth *kOffSet;
    [UIView animateWithDuration:0.3 animations:^{
        _mainView.frame = mainFrame;
        self.leftView.userInteractionEnabled = NO;
    } completion:^(BOOL finished) {
        self.isOpen = YES;
        self.leftView.userInteractionEnabled = YES;
    }];
    

}
// 关闭抽屉
- (void)close{
[UIView animateWithDuration:0.3 animations:^{
    _mainView.frame = self.view.frame;
    self.leftView.userInteractionEnabled = NO;
} completion:^(BOOL finished) {
    self.isOpen = NO;
    self.leftView.userInteractionEnabled = YES;
    
}];
}

//更换新的mainVC
- (void)setNewMainVC:(UIViewController *)newMainVC{
    if (![self.childViewControllers containsObject:newMainVC]) {
        [self addChildViewController:newMainVC];
    }
    newMainVC.view.frame = self.mainView.frame;
    [self.mainView removeFromSuperview];
    [self.view addSubview:newMainVC.view];
    self.mainView = newMainVC.view;
    [newMainVC.view addGestureRecognizer:self.pan];

}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
